#!/usr/bin/python3 
# -*- coding: utf-8 -*-
"""
@File Name  : picture.py
@Author     : LeeCQ
@Date-Time  : 2019/12/31 19:24

下载图片到 - uFile - /HH/9c/Pic
    文件结构：
        ./pic/type_id/a_id

"""
import sys
sys.path.append('.')

import re
try:
    from _root import *
except:
    from HTTP.Download_9C_2._root import *


class ParseError(Error9C): pass
#


class NineCPic(Root9C):
    def __init__(self, retry=5, timeout=10, debug_level=0, strict_mode=True,  _data_path=DATA_PATH,
                 local_root='./album', remote_root='/HH/9C/Pic/'):
        super().__init__(retry=retry, timeout=timeout, debug_level=debug_level,
                         strict_mode=strict_mode, data_path=_data_path,
                         )
        self.data_dict = None
        self.api_pic = API.pic
        self.local_root = local_root if local_root.endswith('/') else local_root + '/'
        self.remote_root = remote_root if remote_root.endswith('/') else remote_root + '/'
        self.type_choose = ['25', '26', '27', '28', '29', '30']

    # 取得pic的typeID & data
    def pic_allData(self):
        return [_id.setdefault('data', self.read_data(_id['id']))
                for _id in sort_relation if _id['type'] == 'pic'
                ]

    # 获取 - 文件列表
    def pic_oneAlbum(self, data: dict):
        """

        :param data:
        :return:
        """
        n = self.retry
        while n:
            n -= 1
            _, js = self.request_get(self.api_pic + data['a_id'], {})
            if _ >= 0:
                picList = re.findall(r'<a .*?scr=(.*?).*?', js.text)
                if len(picList) > 0:
                    return picList
                else:
                    logger.warning(f'没有解析到图片... 正在重试...')
                    time.sleep(1)
        logger.error(f'经过{self.retry}次重试，依旧没有获取到文件: {data["a_id"]}({data["a_name"]})')
        if self.strictMode:
            raise
        else:
            return []

    # 生成器函数：生成一个相册的所有图片列表。
    def pic_allAlbum(self):
        """

        :return: 数据列表
        """
        for typeData in self.pic_allData():
            if typeData['id'] in self.type_choose:
                for data in typeData:
                    yield self.pic_oneAlbum(data)

    #



if __name__ == '__main__':
    a = NineCPic()
    b = a.pic_allData()
    print(b, len(b))
